package com.jiamao.goplay.ui.login;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.gyf.immersionbar.ImmersionBar;
import com.jiamao.goplay.BR;
import com.jiamao.goplay.R;
import com.jiamao.goplay.app.AppViewModelFactory;
import com.jiamao.goplay.base.BaseMVVMActivity;
import com.jiamao.goplay.databinding.LoginRetrievePwdEditLayoutBinding;
import com.jiamao.goplay.ui.login.vm.RetrievePwdEditViewModel;

public class RetrievePwdEditActivity extends BaseMVVMActivity<LoginRetrievePwdEditLayoutBinding, RetrievePwdEditViewModel> {
    //ActivityLoginBinding类是databinding框架自定生成的,对应activity_login.xml
    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.login_retrieve_pwd_edit_layout;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public RetrievePwdEditViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用LoginViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(RetrievePwdEditViewModel.class);
    }

    @Override
    public void initViewObservable() {
        //监听ViewModel中pSwitchObservable的变化, 当ViewModel中执行【uc.pSwitchObservable.set(!uc.pSwitchObservable.get());】时会回调该方法
        viewModel.uc.pSwitchEvent.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable Boolean aBoolean) {
                //pSwitchObservable是boolean类型的观察者,所以可以直接使用它的值改变密码开关的图标
                if (viewModel.uc.pSwitchEvent.getValue()) {
                    //密码可见
                    //在xml中定义id后,使用binding可以直接拿到这个view的引用,不再需要findViewById去找控件了
                    binding.ivSwichPasswrod1.setImageResource(R.mipmap.show_psw);
                    binding.deLoginPassword1.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    //密码不可见
                    binding.ivSwichPasswrod1.setImageResource(R.mipmap.show_psw_press);
                    binding.deLoginPassword1.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }
        });
        //监听ViewModel中pSwitchObservable的变化, 当ViewModel中执行【uc.pSwitchObservable.set(!uc.pSwitchObservable.get());】时会回调该方法
        viewModel.uc.pSwitchEvent2.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable Boolean aBoolean) {
                //pSwitchObservable是boolean类型的观察者,所以可以直接使用它的值改变密码开关的图标
                if (viewModel.uc.pSwitchEvent2.getValue()) {
                    //密码可见
                    //在xml中定义id后,使用binding可以直接拿到这个view的引用,不再需要findViewById去找控件了
                    binding.ivSwichPasswrod2.setImageResource(R.mipmap.show_psw);
                    binding.deLoginPassword2.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    //密码不可见
                    binding.ivSwichPasswrod2.setImageResource(R.mipmap.show_psw_press);
                    binding.deLoginPassword2.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        ImmersionBar.with(this)
                .titleBar(binding.statusView)
                .statusBarDarkFont(true)
                .autoDarkModeEnable(true, 0.2f)
                .init();
        binding.deLoginPassword1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                updateEditFoush();
            }
        });

        binding.deLoginPassword2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                updateEditFoush();
            }
        });

    }

    public void updateEditFoush(){
        if (viewModel.pwd.get().length()>=6 && viewModel.pwd.get().equals(viewModel.confpwd)){
            viewModel.btnClickEndbel.set(true);
            binding.deLoginSignLayout.setBackgroundResource(R.drawable.btn_selector_select);
            binding.deLoginSign.setTextColor(Color.WHITE);
        }else {
            viewModel.btnClickEndbel.set(false);
            binding.deLoginSignLayout.setBackgroundResource(R.drawable.btn_selector_default);
            binding.deLoginSign.setTextColor(Color.parseColor("#d0d0d0"));
        }
    }
}
